********************************************************************************
* PRIMARY INVESIGATOR(S): 	Daniel Scott Smith
* VERSION: 					Stata 16.1 IC


*-------------------------------------------------------------------------------
* Set the critical parameters of the computing environment
*-------------------------------------------------------------------------------

	clear all
	
	macro drop _all
	
	set more off
	
	global	directory	`"/Volumes/GoogleDrive/My Drive/02_Stanford/00_Researching/16_SocialScientization/-01_SSH/00_replication/"'
	global	data		"${directory}00_data/"
	
	
*-------------------------------------------------------------------------------
* BAAS data 
* ---------
	/*
	Source:
 	MacLeod, R, and Peter Jeffrey Collins. 1981. The Parliament of Science : 
	The British Association for the Advancement of Science, 1831-1981. 
	Northwood: Science Reviews.
	*/
*-------------------------------------------------------------------------------
		import excel "${data}baas.xlsx", firstrow

		drop attendance 
		
		save "${data}baas.dta", replace

		clear all

		
		
		
*-------------------------------------------------------------------------------
* Fellows data 
* ------------
	/*
	Source:
	Rosenbaum, S. 1984. “The Growth of the Royal Statistical Society.” 
	Journal of the Royal Statistical Society. Series A (General) 147 (2). 
	[Royal Statistical Society, Wiley]: 375–88. doi:10.2307/2981692.
	*/
*-------------------------------------------------------------------------------
		import excel "${data}fellows.xlsx", sheet("Tabelle1") firstrow
		
		save "${data}fellows.dta", replace

		clear all
		
			
			
				
*-------------------------------------------------------------------------------
* Journals data 
* -------------
	/*
	Source:
	Flora, Peter. 1975. “4. Historical Sources of Statistics.” 
	Current Sociology 23 (2). SAGE Publications Ltd: 113–39. 
	doi:10.1177/001139217502300205
	*/
*-------------------------------------------------------------------------------
		import excel "${data}journals.xlsx", sheet("Tabelle1") firstrow
		
		save "${data}journals.dta"
		
		clear all
		

		
		
*-------------------------------------------------------------------------------
* Colonies data 
* ------------
	/*
	Source:
	O’Neill, Aaron. 2020. “Number of Present-Day Countries That Held Part of 
	the British Empire, in Each Year from 1600 to 2000.” 
	https://www.statista.com/statistics/1070352/number-current-countries-in-british-empire/
	*/
*-------------------------------------------------------------------------------
		import excel "${data}colonies.xlsx", sheet("Tabelle1") firstrow
		destring year, replace

		
		
		
	* Merge BAAS, Fellows, and Journals Data 
	
		merge 1:1 year using "${data}baas.dta"
		drop _merge

		
		merge 1:1 year using "${data}fellows.dta"
		drop _merge
		
		
		merge 1:1 year using "${data}journals.dta" 
		drop _merge
		
		erase "${data}baas.dta"
		erase  "${data}journals.dta"
		erase "${data}fellows.dta"


		
		
*-------------------------------------------------------------------------------
* International Statistics Congress & Institute 

	/*
	Flora, Peter. 1975. “4. Historical Sources of Statistics.” 
	Current Sociology 23 (2). SAGE Publications Ltd: 113–39. 
	doi:10.1177/001139217502300205
	*/
*-------------------------------------------------------------------------------
		
		
	* Enter congresses by hand (p. 126):
	
	
	* Generate indicator of congress meeting
	
		cap drop congress
		gen congress = 0

		replace congress = 1 if 	year == 1853 | ///
										year == 1855 | ///
										year == 1857 | ///
										year == 1860 | ///
										year == 1863 | ///
										year == 1867 | ///
										year == 1869 | ///
										year == 1872 | ///
										year == 1876

								
								
	* Compute running sums of congress meetings	
						
		cap drop congress_all
		gen 	congress_all = 0
		replace congress_all = 1	if 	year >= 1853 & year < 1855
		replace congress_all = 2	if 	year >= 1855 & year < 1857
		replace congress_all = 3	if 	year >= 1857 & year < 1860
		replace congress_all = 4	if 	year >= 1860 & year < 1863
		replace congress_all = 5	if 	year >= 1863 & year < 1867
		replace congress_all = 6	if 	year >= 1867 & year < 1869
		replace congress_all = 7	if 	year >= 1869 & year < 1872
		replace congress_all = 8	if 	year >= 1872 & year < 1876
		replace congress_all = 9	if 	year >= 1876 


		
	* Generate indicator of institute session
	
		cap drop session
		cap drop session_all
		gen	session	= 0 
		gen session_all = 0
		local j = 0

		
	* Generate running sums of institute sessions 
	
		forv i = 1887(2)1913 {
			local j = `j' + 1 
			replace session = 1 if year == `i'
			replace session_all = session_all + `j' if year >= `i' & year < `i' + 2
			}


			
	* Generate total sum 
	
		gen int_meetings = congress_all + session_all
		
		
		
		
	* save temp data
	
		save "${data}temp_iv.dta", replace
	
	
	
	
*-------------------------------------------------------------------------------
* Vdem data
*-------------------------------------------------------------------------------
	
	* Import raw vdem 10 data, pre-selected measures
	* These variables were selected using pandas on Python
	* see notebook cr_west_vdem10

		use	"${data}west_vdem10.dta"
		drop index		


	* Rename variables
	
		rename v2x_* *
		rename v2* *
		rename v3* *
		rename e_* *

		rename	regiongeo	un_region
		rename	exnamhog	hog_name
		rename 	extithog	hog_title
		rename	lpname		maj_party

		rename	suffr 		suffrage
		rename	gencs		wmn_civsoc
		rename	liberal 	liblism
		rename	peprisch	sch_enrll
		rename	migdppc		gdppc
		rename	migdpgro	gdpgrow
		rename	miurbani	urbnztn

		rename	stcitlaw	ctznshp 
		rename	stflag		flag
		rename	stnatant	anthem
		rename	stnatbank	bank

		rename	stcensus	census
		rename	ststatag	agency
		rename	ststybcov	yrbkcov
		rename	ststybpub	yrbkpub


		rename	miinteco	int_cnflct
		rename	miinterc	dmst_cnflct
		
		
		rename	struinvadm 	admin_aut
		rename	ststeecap 	econ_cap

		
		
*-------------------------------------------------------------------------------
* Generate new census variable 			
*-------------------------------------------------------------------------------
			
	* Generate sum of all censuses ever taken

		cap drop census_all
		by country_name (year), sort: gen census_all = sum(census)
		
		
	* Generate indicator of first ever census

		cap drop census_ever
		by country_name (year), sort:	gen census_ever = census_all == 1 ///
										& census_all[_n - 1] != census_all
	
	* Generate indicator if there was ever a census conducted
										
		cap drop census_ever_all
		by country_name (year), sort: gen census_ever_all = sum(census_ever)

		
	* Drop & rename
	
		cap drop census_ever census
		rename census_ever_all census_ever


		
*-------------------------------------------------------------------------------			
* Take out UK data for later use
*-------------------------------------------------------------------------------

		preserve
			keep if country_name == "United Kingdom"
			drop country_name
			drop histname
			rename * uk_*
			rename uk_year year
			save "${data}uk_vdem10.dta", replace
		restore


		
*-------------------------------------------------------------------------------
* Compute west-wide institutional characteristics 
*-------------------------------------------------------------------------------
	
		drop if country_name == "United Kingdom" 

		global	world	suffrage ///
						gdppc ///
						gdpgrow	///
						wmn_civsoc ///
						liblism ///
						sch_enrll ///
						ctznshp ///
						flag ///
						anthem ///
						bank ///
						census_ever ///
						agency ///
						yrbkcov ///
						yrbkpub ///
						int_cnflct ///
						dmst_cnflct ///
						admin_aut	///
						econ_cap
		 
		 
		* Take annual averages / proportions
			
			foreach var in $world {
				cap drop world_`var'
				egen wst_`var' = mean(`var'), by(year)
				}
					

		* Count states
		
			cap drop wst_n_states
			egen wst_n_states = count(country_name), by(year)
					
					
					
		* Create year obs dataset 
		
			keep year wst* 
			egen year_tag = tag(year)
			keep if year_tag == 1
			drop year_tag

			
*-------------------------------------------------------------------------------
* Merge with uk vdem data
*-------------------------------------------------------------------------------

	merge 1:1 year using "${data}uk_vdem10.dta"
	drop _merge
	merge 1:1 year using "${data}temp_iv.dta"
	drop _merge
		
		save "${data}uk_ivs.dta", replace
		erase "${data}uk_vdem10.dta"
		erase "${data}temp_iv.dta"

